package com.yizhuoyan.his.serviceimpl.department;

import com.yizhuoyan.his.ao.DepartmentAo;
import com.yizhuoyan.his.qco.DepartmentQueryQco;
import com.yizhuoyan.his.dao.HosDepartmentDao;
import com.yizhuoyan.his.entity.HosDepartmentEntity;
import com.yizhuoyan.his.service.department.DepartmentManageService;
import com.yizhuoyan.his.servicesupport.DatabaseIdGenerateSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DepartmentManageServiceImpl implements DepartmentManageService {

    @Autowired
    DatabaseIdGenerateSupportService databaseIdGenerateSupportService;
    @Autowired
    HosDepartmentDao dao;

    @Override
    public void addDepartment(DepartmentAo ao) throws Exception {

        HosDepartmentEntity entity=new HosDepartmentEntity();
        entity.setCategoryId(ao.getCategoryId());
        entity.setName(ao.getName());
        entity.setCode(ao.getCode());
        entity.setId(databaseIdGenerateSupportService.nextId());
        entity.setTypeConstantId(ao.getTypeConstantId());
        dao.insert(entity);

    }

    @Override
    public void modifyDepartment(@NotNull Long id,DepartmentAo ao) throws Exception {

        Map<String,Object> map=new HashMap<>();
        map.put("name",ao.getName());
        map.put("code",ao.getCode());
        map.put("category",ao.getCategoryId());
        map.put("typeConstantId",ao.getTypeConstantId());
        dao.update(id,map);

    }

    @Override
    public List<HosDepartmentEntity> queryDepartments(DepartmentQueryQco qco) throws Exception {
        List<HosDepartmentEntity> result=dao.selectByDemands(qco);
        return result;
    }

    @Override
    public void removeDepartmentById(@NotNull Long id) throws Exception {
        dao.delete(id);

    }

    @Override
    public List<HosDepartmentEntity> queryDepartmentByTypeConstantId(@NotNull Long typeConstantId) throws Exception {
        List<HosDepartmentEntity> result=dao.selectByTypeConstantId(typeConstantId);
        return result;

    }

    @Override
    public List<HosDepartmentEntity> queryDepartmentByName(@NotNull String name) throws Exception {
        List<HosDepartmentEntity> result=dao.selectByName(name);
        return result;
    }

    @Override
    public List<HosDepartmentEntity> queryDepartmentByCode(@NotNull String code) throws Exception {
        List<HosDepartmentEntity> result=dao.selectByCode(code);
        return result;
    }

    @Override
    public List<HosDepartmentEntity> queryDepartmentByCategoryId(@NotNull Long categoryId) throws Exception {
        List<HosDepartmentEntity> entity=dao.selectByCategoryId(categoryId);

        return entity;
    }
}
